﻿
using hcache;
using hlog;
using hq;
using System;
using System.Collections.Generic;
 namespace htaskCommon {
    public abstract class AbsDirectionChange :ZtTask {
         public abstract int  getDir1First();
        public abstract int getDir1Next();
        private int dir;
         private int carrierContr1 = 1;
       
        public bool canIn() {
            if( carrierContr1 != 1 ) {
                return false;
            }
            return dir == 1;
        }
        public bool canOut() {
            if( carrierContr1 != 1 ) {
                return false;
            }
            return dir == 2;
        }
         public abstract int getDbModel();
  
        public abstract List<TaskCarrier> getWorkingCarrierTask();
      
        public abstract bool willInstock();
          public abstract int ReadDir();
        public abstract bool hasTask();
         public abstract void WriteDir(short mode);
        public override void excute() {
            dir = ReadDir();
            bool b = hasTask();
            if( b ) {
                LoggerCommon.consol(this.className + "层有任务，当前方向为  " + dir);
                return;
            }
            int Mode1 = getDbModel();
            List<TaskCarrier> taskCarriers = getWorkingCarrierTask ( );
            bool isIn = false;
            bool isOut = false;
            if( willInstock ( ) )//入库口放货需要入库
            {
                isIn = true;
            }
             int dir1First = getDir1First();
            int dir1Next = getDir1Next();
             foreach( TaskCarrier ct in taskCarriers ) {
                List<int> orderList= TransferConfigFacade.ariveOrder(ct.src ,dir1First ,dir1Next ,ct.des);
                if( orderList.Count == 2 ) {
                    if( dir1First == orderList[0] ) {
                        isIn = true;
                    } else {
                        isOut = true;
                    }
                } 
                
                if( isIn && isOut ) {
                    return;
                }
            }
            isOut = otherNeedOut(isIn ,isOut ,Mode1);
            if ( isOut && isIn ) {//口没有被取走 或者 刚刚放上去 即既有入库有用出口 这不变
                return;
            }
        
            changeDir ( isIn ,isOut ,Mode1 );
        }
          protected virtual  bool otherNeedOut(bool neadIn ,bool neadOut ,int carrierMode1) {
            return neadOut;
        }
        public void changeDir(bool neadIn ,bool neadOut ,int carrierMode1) {
            if( carrierMode1 == 1 ) {
                if( !neadOut ) {
                    changeMode ( 1 );
                }
            } else if( carrierMode1 == 2 ) {
                if( !neadIn ) {
                    changeMode ( 2 );
                }
            } else if( carrierMode1 == 3 ) {
                if( neadIn ) {
                    changeMode ( 1 );
                } else if( neadOut ) {
                    changeMode ( 2 );
                } else {
                    changeMode ( 1 );
                }
             }
        }
        void changeMode(short mode) {
             if( dir == mode ) {
                LoggerCommon.consol (this.className + "层无需切换，当前方向为  " +dir);
                return;
            }
            carrierContr1 = 0;
            WriteDir ( mode );
            dir= ReadDir ( );
            carrierContr1 = 1;
            if( dir == mode ) {
                 LoggerCommon.fileAll ( "切换到！" + mode );
            } else {
                LoggerCommon.consol ( "切换失败！" + dir );
            }
         }
     }
}
